import {
  getRecordDetail,
  userPayment,
  createShare
} from '../../../utils/networkRequest.js'
// pages/jiechedanbox/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: null,

    isShowReckoning: false, //是否显示选择施工项目的弹窗


    orderId: "", //订单id
    ppid: "",

    servicesItems: [], //选择施工项目时 的项目列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    wx.hideShareMenu({})
    //要先判断该订单是否允许创建分享  
    let temp = '3_' + options.id;
    createShare(temp, (r) => {
      if (r.code == 0) {
        self.setData({
          ppid: r.data.ppid
        })

      }
    }) 
    self.setData({
      orderId: options.id,
    }) 

  },
  onShow: function() {
    let self = this;
    getRecordDetail(this.data.orderId, (r) => {

      if (r.code == 0) {
        self.setData({
          orderDetail: r.data,
          // currentPayMethod: r.data.payments[0]
        })
      }
    })
  },


  //获取延迟推送天数
  getDelayChange: function(e) {
    this.setData({
      delayNum: e.detail.detail.value
    })
  },


  //跳转至评价
  goEvaluate: function(e) {
    let mid = e.currentTarget.dataset.mid;
    wx.navigateTo({
      url: '../evaluate/evaluate?mid=' + mid + "&rid=" + this.data.orderId,
    })
  },

  //立即付款:
  goPayment: function(e) {
    let self = this;
    let id = e.currentTarget.dataset.id;
    userPayment("服务项目", 3, id, (r) => {
      if (r.code == 0) {
        wx.requestPayment({
          'timeStamp': r.data.timeStamp,
          'nonceStr': r.data.nonceStr,
          'package': r.data.package,
          'signType': r.data.signType,
          'paySign': r.data.paySign,
          'success': function(res) {
            // wx.redirectTo({
            //   url: '../../usercenter/usercenter',
            // })

            getRecordDetail(self.data.orderId, (r) => {
              if (r.code == 0) {
                self.setData({
                  orderDetail: r.data,
                  isShowReckoning: true
                })
              }
            }) 

          },
          'fail': function(res) {}
        })
      }
    })
  },

  //取消订单
  orderCancel: function() {
    let {
      orderDetail
    } = this.data;

    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗？',
      success: function(res) {
        if (res.confirm) {
          orderCancel(orderDetail.id, (r) => {
            if (r.code == 0) {
              wx.navigateBack();
              wx.showToast({
                icon: "none",
                title: '取消成功',
              })
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })
  },
  //关闭结算窗口
  onReckoningClose: function() {
    this.setData({
      isShowReckoning: false
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {


    if (res.from === 'button') {
      // 来自页面内转发按钮
    
    }

    return {
      title: '快来助力我，获取免费洗车券。',
      path: 'pages/sharebox/xiche/xiche?ppid=' + this.data.ppid,
      imageUrl: 'http://www.haoche666.com/api/app_pics_url?name=share_chat'
    }

    
  }


})